###########################################################
# $Id: xrsh_host.sh 4438 2012-12-21 03:03:54Z xrdk $
# brief	    : 
# author    : xrdk
# version   : V1.0, 2004-03-16
#
# CopyRight (C) 2004-2011 xrdk.
# All rights reserved.
###########################################################

function xrmk_host_supped()
{
	local _xrmk_arr=(lnx aix sun hpux osx freebsd qnx)
	
	if [ "$1" = "get" ]; then
		echo Host supped: ${_xrmk_arr[*]}
		return
	fi
	
	local _xrmk_tmp=`echo ${_xrmk_arr[*]}`
	_xrmk_tmp=`xrsh_hasitem "$_xrmk_tmp" $1`
	
	if test "$_xrmk_tmp" = "no"; then
		echo "Unsupp host: $1"
		xrmk_host_supped "get"
		exit 1
	fi
}

function xrmk_host_echo()
{
	echo ---------- host info ------------------------
	echo Host: $xrmk_host_name $xrmk_host_vdot
	echo "--       version : $xrmk_host_vmaj $xrmk_host_vmin $xrmk_host_vnum"
	echo "--    spec. name : $xrmk_host_specname"
	echo "-- spec. version : $xrmk_host_specver"
	echo "--         arch. : $xrmk_host_arch"
	echo "--   arch. is 64 : $xrmk_host_arch64"
  echo "--      dso ext. : $xrmk_host_dsoext"
	echo "--      lib ext. : $xrmk_host_libext"
  echo --------------------------------------------
}
#=====================================================================
function xrmk_host_get()
{
	local _xrmk_tmp=$1
	case $_xrmk_tmp in
	  lnx|linux)  xrmk_get_host_lnx	    ;;
	  sun)	      xrmk_get_host_sun	    ;;
	  aix)	      xrmk_get_host_aix	    ;;
	  hpux)	      xrmk_get_host_hpux    ;;
	  osx)	      xrmk_get_host_osx	    ;;
	  freebsd)	  xrmk_get_host_freebsd ;;
	  qnx)	      xrmk_get_host_qnx	    ;;
	  *)	    ;;
	esac
}

function xrmk_host_ver_normal()
{
	if [ "$xrmk_host_vmaj" = "" ] && [ "$xrmk_host_vmin" != "" ]; then xrmk_host_vmin= ; fi
	if [ "$xrmk_host_vmin" = "" ] && [ "$xrmk_host_vnum" != "" ]; then xrmk_host_vnum= ; fi
	
	if [ "$xrmk_host_vdot" = "" ] && [ "$xrmk_host_vmaj" != "" ]; then
		xrmk_host_vdot="$xrmk_host_vmaj"
		if [ "$xrmk_host_vmin" != "" ]; then xrmk_host_vdot="$xrmk_host_vdot"."$xrmk_host_vmin" ; fi
		if [ "$xrmk_host_vnum" != "" ]; then xrmk_host_vdot="$xrmk_host_vdot"."$xrmk_host_vnum" ; fi
	fi
}

#Get host name, arch, ver., ...
function xrmk_host_get_ex()
{
	xrmk_host_name=`xrmk_host_get_name_ex`
	xrmk_host_arch=`xrmk_host_get_arch_ex`
	
	xrmk_host_supped $xrmk_host_name
	xrmk_host_get $xrmk_host_name
	
	if test "$xrmk_host_arch64" = ""; then xrmk_host_arch64=`xrsh_isarch64 $xrmk_host_arch`; fi
	if test "$xrmk_host_dsoext" = ""; then xrmk_host_dsoext=".so"; fi
	if test "$xrmk_host_libext" = ""; then xrmk_host_libext=".a"; fi
	
	xrmk_host_ver_normal
}
#=====================================================================
#=====================================================================
#Get host name
function xrmk_host_get_name()
{
	uname -s
}

#Get host name and convert
function xrmk_host_get_name_ex()
{
	local _xrmk_tmp=`xrmk_host_get_name`
	echo `xrsh_cvt_osname $_xrmk_tmp`
}

#Get host arch. Args: 1 os name
function xrmk_host_get_arch()
{
	local _xrmk_tmp=`xrsh_cvt_osname $1`
	if test "$_xrmk_tmp" = "qnx" || test "$_xrmk_tmp" = "hpux"; then
		uname -p
	else
		uname -m
	fi
}

#Get host arch and convert
function xrmk_host_get_arch_ex()
{
	local _xrmk_tmp=`xrmk_host_get_name`
	_xrmk_tmp=`xrmk_host_get_arch $_xrmk_tmp`
	echo `xrsh_cvt_archname $_xrmk_tmp`
}
#=====================================================================
function xrmk_get_host_lnx()
{
	#local _xrmk_tmp=`uname -r | sed -n 's/\([0-9]\{1,10\}\.[0-9]\{1,10\}\.[0-9]\{1,10\}\) *.*/\1/p'`
	local _xrmk_tmp=`uname -r`
	_xrmk_tmp=`xrsh_getdotnumstr $_xrmk_tmp`
	if test "$_xrmk_tmp" != ""; then
	  xrmk_host_vmaj=`xrsh_getdotnum $_xrmk_tmp 1`
	  xrmk_host_vmin=`xrsh_getdotnum $_xrmk_tmp 2`
	  xrmk_host_vnum=`xrsh_getdotnum $_xrmk_tmp 3`
	else
	  xrmk_host_vdot=`uname -r`
	fi
	
	if test "$xrmk_host_specname" = ""; then
	  _xrmk_tmp=`uname -r | sed -n 's/.*\.el\([0-9]\{1,10\}\) *.*/\1/p'`
	  if test "$_xrmk_tmp" != ""; then
	    xrmk_host_specname=rhel
	    xrmk_host_specver=$_xrmk_tmp
	  fi
	fi

	if test "$xrmk_host_specname" = ""; then
	  _xrmk_tmp=`uname -v | tr '[A-Z]' '[a-z]' | sed -n 's/#\([0-9]\{1,10\}\)\-ubuntu *.*/\1/p'`
	  if test "$_xrmk_tmp" != ""; then
	    xrmk_host_specname=ubuntu
	    xrmk_host_specver=$_xrmk_tmp
	  fi
	fi

	if test "$xrmk_host_specname" = ""; then
	  _xrmk_tmp=`uname -r | sed -n 's/.*\.fc\([0-9]\{1,10\}\) *.*/\1/p'`
	  if test "$_xrmk_tmp" != ""; then
	    xrmk_host_specname=fedora
	    xrmk_host_specver=$_xrmk_tmp
	  fi
	fi
}
#=====================================================================
function xrmk_get_host_sun()
{
	local _xrmk_tmp=`uname -r`
	xrmk_host_vmaj=`xrsh_getdotnum $_xrmk_tmp 1`
	xrmk_host_vmin=`xrsh_getdotnum $_xrmk_tmp 2`
	_xrmk_tmp=`xrmk_host_get_arch_ex`
	if [ "$_xrmk_tmp" = "sparc" ] && [ $xrmk_host_vmaj$xrmk_host_vmin -gt "58" ]; then
		xrmk_host_arch64=yes
	fi
}
#=====================================================================
function xrmk_get_host_aix()
{
	xrmk_host_vmaj=`uname -v`
	xrmk_host_vmin=`uname -r`
}
#=====================================================================
function xrmk_get_host_osx()
{
	#local _xrmk_tmp=`uname -r | sed -n 's/\([0-9]\{1,10\}\.[0-9]\{1,10\}\.[0-9]\{1,10\}\) *.*/\1/p'`
	local _xrmk_tmp=`uname -r`
	_xrmk_tmp=`xrsh_getdotnumstr $_xrmk_tmp`
	
	xrmk_host_vmaj=`xrsh_getdotnum $_xrmk_tmp 1`
	xrmk_host_vmin=`xrsh_getdotnum $_xrmk_tmp 2`
	xrmk_host_vnum=`xrsh_getdotnum $_xrmk_tmp 3`
	  
	xrmk_host_dsoext=".dylib"
}
#=====================================================================
function xrmk_get_host_hpux()
{
	#local _xrmk_tmp=`uname -r | sed -n 's/\([0-9]\{1,10\}\.[0-9]\{1,10\}\) *.*/\1/p'`
	local _xrmk_tmp=`uname -r`
	_xrmk_tmp=`xrsh_getdot2numstr $_xrmk_tmp`
	
	xrmk_host_vmaj=`xrsh_getdotnum $_xrmk_tmp 1`
	xrmk_host_vmin=`xrsh_getdotnum $_xrmk_tmp 2`
	  
	_xrmk_tmp=`xrmk_host_get_arch_ex`
	if test "$_xrmk_tmp" = "ia64"; then
	  xrmk_host_dsoext=".sl"
	fi
}
#=====================================================================
function xrmk_get_host_freebsd()
{
	#local _xrmk_tmp=`uname -r | sed -n 's/\([0-9]\{1,10\}\.[0-9]\{1,10\}\) *.*/\1/p'`
	local _xrmk_tmp=`uname -r`
	_xrmk_tmp=`xrsh_getdot2numstr $_xrmk_tmp`
	
	xrmk_host_vmaj=`xrsh_getdotnum $_xrmk_tmp 1`
	xrmk_host_vmin=`xrsh_getdotnum $_xrmk_tmp 2`
}
#=====================================================================
function xrmk_get_host_qnx()
{
	#local _xrmk_tmp=`uname -r | sed -n 's/\([0-9]\{1,10\}\.[0-9]\{1,10\}\.[0-9]\{1,10\}\) *.*/\1/p'`
	local _xrmk_tmp=`uname -r`
	_xrmk_tmp=`xrsh_getdotnumstr $_xrmk_tmp`

	if test "$_xrmk_tmp" != ""; then
		xrmk_host_vmaj=`xrsh_getdotnum $_xrmk_tmp 1`
		xrmk_host_vmin=`xrsh_getdotnum $_xrmk_tmp 2`
		xrmk_host_vnum=`xrsh_getdotnum $_xrmk_tmp 3`
	
	else
	  _xrmk_tmp=`uname -v`
	  xrmk_host_vmaj=`xrsh_getnumplace $_xrmk_tmp 100`
	  xrmk_host_vmin=`xrsh_getnumplace $_xrmk_tmp 10`
	  xrmk_host_vnum=`xrsh_getnumplace $_xrmk_tmp`
	fi
}
#=====================================================================
#=====================================================================

